<template>
	<view class="addParentMessage">
		<u-navbar :title="navTitle" :autoBack="true" :placeholder="true">
		</u-navbar>

		<view v-if="type == 1" class="">
			<view class="cartTitle">
				学生信息
			</view>

			<view class="contentBox">
				<view class="lable">
					学校名称
				</view>
				<view class="inputBox">
					<uni-combox :candidates="schoolList" placeholder="请选择学校名称"></uni-combox>
					<image src="https://mp.blrwworkshop.com/imgs/low.png.png" mode="">
					</image>
				</view>
				<view class="lable">
					年级班级
				</view>
				<view class="inputBox" style="margin-bottom: 0;" @click="handleclassShow">
					<view class="inputPolicehoder">
						{{student1.className || "选择学生所属年级班级"}}
					</view>
					<image src="https://mp.blrwworkshop.com/imgs/low.png.png" mode=""></image>
				</view>

				<view class="lable" style="margin-top: 20rpx;">
					学生姓名
				</view>
				<view class="inputBox">
					<input class="input" v-model="student1.name" placeholder="输入学生姓名" />
				</view>
				<view class="lable">
					学生性别
				</view>
				<view class="inputBox" @click="handleSexShow(1)">
					<view class="inputPolicehoder" style="padding-left: 20rpx;">
						{{student1.sexName ||  '请选择学生性别' }}
					</view>
					<image src="https://mp.blrwworkshop.com/imgs/low.png.png" mode=""></image>
				</view>
			</view>



		</view>



		<view v-if="type == 2" class="">
			<view class="cartTitle">
				家长信息
			</view>
			<view class="contentBox">
				<view class="lable">
					家长姓名
				</view>
				<view class="inputBox">
					<input class="input" v-model="parentsName" placeholder="输入家长姓名" />
				</view>
				<view class="lable">
					与学生关系
				</view>
				<view class="inputBox" style="padding-left: 15rpx;" @click="identityShow = true">
					{{identityName || "请选择与家长关系"}}
					<image src="https://mp.blrwworkshop.com/imgs/low.png.png" mode=""></image>
				</view>

				<view class="lable">
					家长手机号
				</view>
				<view class="inputBox" style="padding-left: 15rpx;">
					<input class="input" type="number" v-model="moblieParent" placeholder="输入家长手机号" />
				</view>
			</view>
		</view>


		<view class="btns" :class="{btns_student: role==1,btns_parent:role==2}" @click="handleSubmit">
			提交
		</view>

		<!-- 班级年级 -->
		<u-picker :show="classShow" ref="uPicker" confirmColor="#FFA116" :columns="classList"
			@cancel="classShow = false" @change="changeClassList" @confirm="confirmClassList"></u-picker>

		<!-- 性别 -->
		<u-picker :show="sexshow" keyName="name" confirmColor="#FFA116" :columns="sexList" @cancel="sexshow = false"
			@confirm="confirmSex"></u-picker>

		<!-- 关系 -->
		<u-picker :show="identityShow" keyName="name" confirmColor="#FFA116" :columns="ParentList"
			@cancel="identityShow = false" @confirm="confirmParent"></u-picker>

	</view>
</template>

<script>
	import {
		listAllSchool,
		qryGradeClazzTree,
	} from "@/api/api.js"
	import {
		insertAnotherParents,
		insertAnotherStudent
	} from "@/api/mine.js"
	export default {
		data() {
			return {
				role: uni.getStorageSync("role"),
				navTitle: "填写家长信息",
				type: 1,
				schoolList: ['美言小学', '美言中学', '实验小学', '第一小学', '天津小学'],
				identityShow: false,
				parentsName: "",
				moblieParent: "",
				identityType: "",
				identityName: "",
				ParentList: [
					[{
						name: "爸爸",
						id: 1
					}, {
						name: "妈妈",
						id: 2
					}]
				],
				// ======================下面学生提交信息
				student1: {
					schoolId: "",
					clazzId: "",
					className: "",
					name: "",
					sex: null,
					sexName: "",
				},
				schoolList: [],
				classShow: false,
				classData: [],
				classList: [
					[],
					[]
				],
				sexshow: false,
				sexList: [
					[{
						name: "男",
						id: 0
					}, {
						name: "女",
						id: 1
					}, ]
				]

			}
		},
		onLoad(options) {
			this.type = options.type
			if (options.type == 1) {
				this.navTitle = "填写学生信息"
				this.handleGetlistAll()
			} else {
				this.navTitle = "填写家长信息"
			}
			let that = this
			uni.$on("changeSchool", (data) => {
				// console.log("changeSchool", data)
				that.student1.schoolId = data.schoolId
				that.student1.clazzId = ""
				that.student1.className = ""
				that.handleqryGradeClazzTree()
			})
		},
		methods: {
			//获取学校列表
			async handleGetlistAll() {
				let res = await listAllSchool()
				if (res.code == 200) {
					this.schoolList = res.data
				}
			},
			handleclassShow() {
				if (this.student1.schoolId == "") {
					uni.showToast({
						title: '请先选择学校',
						icon: 'none'
					})
					return
				}
				this.classShow = true
			},
			//获取年假班级树形数据
			async handleqryGradeClazzTree() {
				let res = await qryGradeClazzTree({
					schoolId: this.student1.schoolId,
					// schoolId: 3
				})
				if (res.code == 200) {
					this.classData = res.data
					this.classList = [
						[],
						[]
					]
					// console.log("年级班级", this.classData[0].clazzList)
					res.data.forEach((item, index) => {
						this.classList[0].push(item.gradeName)
					})
					this.classData[0].clazzList.forEach((item, index) => {
						this.classList[1].push(item.clazzName)
					})
				}
			},
			confirmClassList(e) {
				// console.log('confirm', e)
				this.student1.className = e.value.join('/')
				this.student1.clazzId = this.classData[e.indexs[0]].clazzList[e.indexs[1]].clazzId
				this.classShow = false
				// console.log("this.student1.clazzId ", this.student1.clazzId)
			},
			changeClassList(e) {
				const {
					columnIndex,
					index,
					// 微信小程序无法将picker实例传出来，只能通过ref操作
					picker = this.$refs.uPicker
				} = e

				if (columnIndex == 0) {
					this.classList[1] = []
					this.classData[index].clazzList.forEach((item, index) => {
						this.classList[1].push(item.clazzName)
					})
					this.$forceUpdate()
					// console.log("classList", this.classList[1])
				}
			},

			handleSexShow(value) {
				if (value == 1) {
					this.sexshow = true
				}
			},
			confirmSex(e) {
				// console.log("性别", e)
				this.student1.sex = e.indexs[0]
				this.student1.sexName = e.value[0].name
				this.sexshow = false
			},
			// ================
			confirmParent(e) {
				this.identityType = e.value[0].id
				this.identityName = e.value[0].name
				this.identityShow = false
				// console.log("e.indexs[0].id", e.indexs[0].id)
			},
			handleSubmit() {
				if (this.type == 1) {
					this.handleInsertAnotherStudent()
				}
				if (this.type == 2) {
					this.handleInsertAnotherParents()
				}
			},
			async handleInsertAnotherStudent() {
				if (this.student1.schoolId == "") {
					uni.showToast({
						title: '请先选择学校',
						icon: 'none'
					})
					return
				}
				if (this.student1.clazzId == "") {
					uni.showToast({
						title: '请先选择学生所属年级班级',
						icon: 'none'
					})
					return
				}
				if (this.student1.name == "") {
					uni.showToast({
						title: '请先输入学生姓名',
						icon: 'none'
					})
					return
				}
				if (this.student1.sex == null) {
					uni.showToast({
						title: '请先选择学生性别',
						icon: 'none'
					})
					return
				}
				let data = {
					schoolId: this.student1.schoolId,
					clazzId: this.student1.clazzId,
					name: this.student1.name,
					sex: this.student1.sex,
				}
				let res = await insertAnotherStudent(data)
				if (res.code == 200) {
					uni.showToast({
						title: '添加成功',
						icon: 'none'
					})
					setTimeout(() => {
						uni.navigateBack()
					}, 1500)
				} else {
					res.msg = res.msg.replace(/<br\/?>/g, '\n');
					uni.showModal({
						title: '提示',
						content: res.msg,
						showCancel: false
					});
				}

			},
			async handleInsertAnotherParents() {
				if (this.parentsName == '') {
					uni.showToast({
						title: '请先输入家长姓名',
						icon: 'none'
					})
					return
				}
				if (this.identityType == '') {
					uni.showToast({
						title: '请先选择家长关系',
						icon: 'none'
					})
					return
				}
				if (this.moblieParent == '') {
					uni.showToast({
						title: '请先输入家长手机号',
						icon: 'none'
					})
					return
				}
				const cnPattern = /-9]\d{9}$/;
				if (!/^1[3-9]\d{9}$/.test(this.moblieParent)) {
					uni.showToast({
						title: '请输入正确的家长手机号',
						icon: 'none'
					})
					return
				}
				let data = {
					name: this.parentsName,
					phone: this.moblieParent,
					identityType: this.identityType
				}
				let res = await insertAnotherParents(data)
				if (res.code == 200) {
					uni.showToast({
						title: '添加成功',
						icon: 'none'
					})
					setTimeout(() => {
						uni.navigateBack()
					}, 1500)
				} else {
					res.msg = res.msg.replace(/<br\/?>/g, '\n');
					uni.showModal({
						title: '提示',
						content: res.msg,
						showCancel: false
					});
				}

			},

		}
	}
</script>

<style scoped lang="scss">
	.addParentMessage {
		width: 100%;
		min-height: 100vh;
		background-color: #F9F9F9;

		.cartTitle {
			font-weight: 600;
			font-size: 36rpx;
			color: #333333;
			line-height: 50rpx;
			width: 100%;
			padding: 0 48rpx;
			box-sizing: border-box;
			margin-top: 26rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;

			image {
				width: 48rpx;
				height: 48rpx;
			}
		}

		.contentBox {
			width: 690rpx;
			background: #FFFFFF;
			box-shadow: 0rpx 0rpx 16rpx 0rpx rgba(255, 192, 143, 0.19);
			border-radius: 20rpx;
			margin: 16rpx auto 0;
			padding: 52rpx 30rpx;
			box-sizing: border-box;

			.lable {
				font-weight: 400;
				font-size: 32rpx;
				color: #333333;
				line-height: 44rpx;
				margin-bottom: 16rpx;
			}

			.dropdown {
				position: absolute;
				left: 0;
				right: 0;
				top: 80rpx;
				background: #fff;
				border: 1rpx solid #eee;
				border-radius: 0 0 10rpx 10rpx;
				z-index: 99;
				max-height: 300rpx;
				overflow-y: auto;
				box-shadow: 0 4rpx 16rpx 0 rgba(0, 0, 0, 0.08);
				padding-bottom: 20rpx;

				.dropdown-item {
					padding: 20rpx 24rpx;
					font-size: 28rpx;
					color: #333;
					// border-bottom: 1rpx solid #f3f3f3;
				}

				.dropdown-item:last-child {
					border-bottom: none;
				}
			}

			.inputBox {
				width: 626rpx;
				height: 80rpx;
				border-radius: 10rpx;
				border: 2rpx solid #F3F3F3;
				margin-bottom: 25rpx;
				position: relative;
				display: flex;
				align-items: center;
				justify-content: space-between;
				font-size: 28rpx;

				.selectGx {
					padding-left: 20rpx;
					box-sizing: border-box;
				}

				.input {
					width: 100%;
					height: 60%;
					padding-left: 20rpx;
					font-weight: 400;
					font-size: 28rpx;
					line-height: 40rpx;
				}

				image {
					width: 44rpx;
					height: 44rpx;
				}

				.dropdown {
					position: absolute;
					left: 0;
					right: 0;
					top: 80rpx;
					background: #fff;
					border: 1rpx solid #eee;
					border-radius: 0 0 10rpx 10rpx;
					z-index: 99;
					max-height: 300rpx;
					overflow-y: auto;
					box-shadow: 0 4rpx 16rpx 0 rgba(0, 0, 0, 0.08);
					padding-bottom: 20rpx;

					.dropdown-item {
						padding: 20rpx 24rpx;
						font-size: 28rpx;
						color: #333;
						// border-bottom: 1rpx solid #f3f3f3;
					}

					.dropdown-item:last-child {
						border-bottom: none;
					}
				}
			}
		}

		.btns {
			width: 636rpx;
			margin-top: 40rpx;
		}

	}
</style>